<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Module.php 82828 2019-04-19 14:44:51 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_module
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;
use ticky\page;

class module extends base {

    // 首页
    public function index() {

        define('INSTALL', true);
        $dirs = $dirs_arr = array();
        $dirs = glob(APP_PATH . '*', GLOB_ONLYDIR);
        foreach ($dirs as $d) {
            if ($d != 'common') {
                $dirs_arr[] = basename($d);
            }
        }
        $total = count($dirs_arr);
        $dirs_arr = array_chunk($dirs_arr, 10, true);
        $page = isset($_GET['p']) ? intval($_GET['p']) : 1;
        $directory = isset($dirs_arr[($page - 1)]) ? $dirs_arr[($page - 1)] : array();
        $page = new page($total, 10);
        $data = db('module')->select();
        foreach ($data as $val) {
            unset($val['setting']);
            unset($val['listorder']);
            unset($val['disabled']);
            unset($val['description']);
            $modules[$val['module']] = $val;
        }
        $list = array();
        foreach ($directory as $val) {
            $item = array();
            if (array_key_exists($val, $modules)) {
                $modules[$val]['exists'] = 1;
                $item = $modules[$val];
            } else {
                $path = APP_PATH . $val . DS . 'install' . DS . 'config.php';
                if (is_file($path)) {
                    $isinstall = 1;
                } else {
                    $isinstall = 0;
                }
                $item = array(
                    'exists' => 0,
                    'module' => $val,
                    'isinstall' => $isinstall,
                    'version' => '未知',
                    'installdate' => '未安装',
                    'updatedate' => '未安装',
                    'path' => $path,
                );
            }
            $list[] = $item;
        }
        //debug($list);
        $this->assign('page', $page);
        $this->assign('modules', $list);

        $this->display('module/index');
    }

    /**
     * 模块安装
     */
    public function install() {
        define('INSTALL', true);
        $module = $_GET['module'];
        $module_api = new \module_api();
        if (isset($_POST['dosubmit'])) {
            if ($module_api->install($module)) {
                cache('menu_string_' . session('roleid'), null);
                showmsg('安装成功！', url('/admin/module/cache'));
            } else {
                showmsg($module_api->error_msg, url('/admin/module/index'));
            }
        } else {
            if (!$module_api->check($module)) {
                showmsg($module_api->error_msg, url('/admin/module/index'), 10);
            }
            $config = include APP_PATH . $module . DS . 'install' . DS . 'config.php';
            if (!is_array($config)) {
                showmsg('配置文件错误！', url('/admin/module/index'), 10);
            }
            $this->assign('config', $config);
            $this->display('module/module_config');
        }
    }

    /**
     * 模块卸载
     */
    public function uninstall() {
        define('UNINSTALL', true);
        if (!isset($_GET['module']) || empty($_GET['module'])) {
            showmsg('模块名称为空！');
        }
        $module_api = new \module_api();
        if ($module_api->uninstall($_GET['module'])) {
            cache('menu_string_1', null);
            showmsg('卸载成功！', url('/admin/module/index'));
        } else {
            showmsg($module_api->error_msg, url('/admin/module/index'));
        }
    }

    /**
     * 更新模块缓存
     */
    public function cache() {
        echo '<script type="text/javascript">parent.location.reload();</script>';
    }

}
